package com.jili.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * ClassName: MyInterceptor
 * Package com.jili.interceptr
 * Description:
 *
 * @Author: 廖继礼
 * @Create: 2025/3/8
 * @Version: v1.0
 */
@Slf4j
public class MyInterceptor implements HandlerInterceptor {

    // 执行handler之前，调用的拦截方法
    // 编码格式设置，登录保护，权限处理

    /**
     *
     * @param request  请求对象
     * @param response 响应对象
     * @param handler  handler就是我们要调用的对象
     * @return  true：放行  false：拦截
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("request is {}", request);
        log.info("response is {}", response);
        log.info("handler is {}", handler);
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    /**
     *   andler执行完毕后，触发的方法，没有拦截机制
     * @param request
     * @param response
     * @param handler  handler方法
     * @param modelAndView  返回的视图和共享域数据对象
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("MyInterceptor postHandle");
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    /**
     * 处理完毕
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("MyInterceptor afterCompletion");
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
